package com.ias.agdis_r.activity;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.view.KeyEvent;

import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.util.NFCUtil;


/**
 * Created by zhangsc on 2016/8/31.
 */
public class BaseKeyActivity extends Activity {
    private NfcAdapter mNfcAdapter;
    private PendingIntent mPendingIntent;
    private IntentFilter[] intentFiltersArray;
    private String[][] techListsArray;

    // 判断双击事件
    private long[] mHits = new long[2];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        try {
            intentFilter.addDataType("*/*");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        intentFiltersArray = new IntentFilter[]{intentFilter};
        techListsArray = new String[][]{new String[]{MifareClassic.class.getName()}, new String[]{Ndef.class.getName()}};
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mNfcAdapter != null) {
            mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, intentFiltersArray, techListsArray);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
            NFCUtil.handleActionNDEF(this, intent, mNfcAdapter);
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == 233) {
            EffectManager.getInstance().setMusicVolumeRaise();
        }else if (keyCode == 234) {
            EffectManager.getInstance().setMusicVolumeLower();
        }else if (keyCode == KeyEvent.KEYCODE_MENU){
            onMenuKeyDown();
        }
        return super.onKeyDown(keyCode, event);
    }

//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
//            System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
//            mHits[mHits.length - 1] = SystemClock.uptimeMillis();
//            if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) {
//                EffectManager.getInstance().ttsCancel();    // 双击屏幕，取消语音播报
//            }
//        }
//        return super.dispatchTouchEvent(ev);
//    }

    protected void onMenuKeyDown(){

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mNfcAdapter != null) {
            mNfcAdapter.disableForegroundDispatch(this);
        }
    }
}
